<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="申请类型" prop="applicationType">
        <el-select v-model="queryParams.applicationType" placeholder="请选择申请类型" clearable>
          <el-option label="大学生科技园退租" value="1" />
          <el-option label="众创空间退出" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item label="企业名称" prop="enterpriseName">
        <el-input
          v-model="queryParams.enterpriseName"
          placeholder="请输入企业名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option label="保存（未提交）" value="0" />
          <el-option label="提交未审核" value="1" />
          <el-option label="审核通过" value="2" />
          <el-option label="审核不通过" value="3" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="exitApplicationList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="申请类型" align="center" prop="applicationType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.applicationType === '1'" type="success">大学生科技园退租</el-tag>
          <el-tag v-else type="warning">众创空间退出</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="企业名称" align="center" width="180">
        <template #default="scope">
          {{ scope.row.applicationType === '1' ? scope.row.enterpriseName : scope.row.enterpriseTeam }}
        </template>
      </el-table-column>
      <el-table-column label="经办人" align="center" width="180">
        <template #default="scope">
          {{ scope.row.applicationType === '1' ? scope.row.contactPerson : scope.row.contactPerson }}
        </template>
      </el-table-column>
      <el-table-column label="联系电话" align="center" width="180">
        <template #default="scope">
          {{ scope.row.applicationType === '1' ? scope.row.contactMethod : scope.row.contactMethod }}
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applicationDate" width="180" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status === 0" type="info">保存（未提交）</el-tag>
          <el-tag v-else-if="scope.row.status === 1" type="warning">提交未审核</el-tag>
          <el-tag v-else-if="scope.row.status === 2" type="success">审核通过</el-tag>
          <el-tag v-else-if="scope.row.status === 3" type="danger">审核不通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button
          >
          <el-button
            v-if="scope.row.status === 1"
            size="mini"
            type="text"
            icon="el-icon-check"
            @click="handleApprove(scope.row)"
          >审核</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="120px">
        <!-- 退租基本信息 -->
        <el-card>
          <div slot="header" class="header">退租基本信息</div>
          <template v-if="queryParams.applicationType === '1'">
            <el-form-item label="申请时间">{{ form.createTime }}</el-form-item>
            <el-form-item label="企业名称">{{ form.enterpriseName }}</el-form-item>
            <el-form-item label="房号">{{ form.roomNumber }}</el-form-item>
            <el-form-item label="经办人">{{ form.contactPerson }}</el-form-item>
            <el-form-item label="联系方式">{{ form.contactMethod }}</el-form-item>
            <el-form-item label="退租原因">{{ form.exitReason }}</el-form-item>
            <el-form-item label="申请退租日期">{{ form.exitDate }}</el-form-item>
            <el-form-item label="退款账户户名">{{ form.refundAccountName }}</el-form-item>
            <el-form-item label="退款账户账号">{{ form.refundAccountNumber }}</el-form-item>
            <el-form-item label="开户行">{{ form.refundBank }}</el-form-item>
            <el-form-item label="承诺人签字">{{ form.applicantSignature }}</el-form-item>
            <el-form-item label="承诺日期">{{ form.commitmentDate }}</el-form-item>
            <el-form-item label="履约保证金">{{ form.deposit }}</el-form-item>
            <el-form-item label="房屋租赁费">{{ form.rentalFee }}</el-form-item>
            <el-form-item label="应退款">{{ form.refundAmount }}</el-form-item>
            <el-form-item label="需补交款">{{ form.additionalPayment }}</el-form-item>
            <el-form-item label="经办人">{{ form.handler }}</el-form-item>
            <el-form-item label="办公室主任核签">{{ form.officeDirectorSignature }}</el-form-item>
            <el-form-item label="部门负责人签字">{{ form.departmentHeadSignature }}</el-form-item>
          </template>
          <template v-else>
            <el-form-item label="申请时间">{{ form.createTime }}</el-form-item>
            <el-form-item label="企业/团队">{{ form.enterpriseTeam }}</el-form-item>
            <el-form-item label="房号及卡座号">{{ form.roomNumber }}</el-form-item>
            <el-form-item label="经办人">{{ form.contactPerson }}</el-form-item>
            <el-form-item label="联系方式">{{ form.contactMethod }}</el-form-item>
            <el-form-item label="退出原因">{{ form.exitReason }}</el-form-item>
            <el-checkbox label="门禁指纹是否清除" v-model="form.accessControlCleared"></el-checkbox>
            <el-checkbox label="公司物品是否清出" v-model="form.companyItemsCleared"></el-checkbox>
            <el-checkbox label="公司注册地址是否迁移" v-model="form.registrationAddressMigrated"></el-checkbox>
            <el-checkbox label="公司是否注销" v-model="form.companyDeregistered"></el-checkbox>
            <el-form-item label="已缴纳保证金金额">{{ form.depositPaid }}</el-form-item>
            <el-form-item label="应退保证金金额">{{ form.depositRefundable }}</el-form-item>
            <el-form-item label="退款账户户名">{{ form.refundAccountName }}</el-form-item>
            <el-form-item label="退款账户账号">{{ form.refundAccountNumber }}</el-form-item>
            <el-form-item label="开户行">{{ form.refundBank }}</el-form-item>
            <el-form-item label="申请人签字">{{ form.applicantSignature }}</el-form-item>
            <el-form-item label="申请人签字日期">{{ form.applicantSignatureDate }}</el-form-item>
            <el-form-item label="应退款金额">{{ form.refundAmount }}</el-form-item>
            <el-form-item label="经办人">{{ form.handler }}</el-form-item>
            <el-form-item label="部门负责人意见">{{ form.departmentHeadOpinion }}</el-form-item>
            <el-form-item label="部门负责人签字日期">{{ form.departmentHeadSignatureDate }}</el-form-item>
          </template>
        </el-card>

        <!-- 审核意见 -->
        <template v-if="queryParams.applicationType === '1'">
          <el-form-item label="审核意见" v-if="form.status === 1">
            <el-input
              type="textarea"
              v-model="form.opinion"
              placeholder="请输入审核意见"
            />
          </el-form-item>
          <el-form-item label="审核意见" v-else>
            <el-input
              type="textarea"
              v-model="form.opinion"
              disabled
            />
          </el-form-item>
        </template>
        <template v-else>
          <el-form-item label="审核意见" v-if="form.status === 1">
            <el-input
              type="textarea"
              v-model="form.opinion"
              placeholder="请输入审核意见"
            />
          </el-form-item>
          <el-form-item label="审核意见" v-else>
            <el-input
              type="textarea"
              v-model="form.opinion"
              disabled
            />
          </el-form-item>
        </template>
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="form.status === 1">
        <el-button type="primary" @click="handleApproveSubmit(2)">审核通过</el-button>
        <el-button type="danger" @click="handleApproveSubmit(3)">审核不通过</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getexitApplicationForm,
  getexitApplicationFormDetail,
  editexitApplicationForm,
  delexitApplicationForm,
  getexitCreateSpaceForm,
  getexitCreateSpaceFormDetail,
  editexitCreateSpaceForm,
  delexitCreateSpaceForm
} from "@/api/system/exit-application";

export default {
  name: "ExitApplication",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请表列表
      exitApplicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        pageSize: 10,
        applicationType: null,
        enterpriseName: null,
        status: null
      },
      // 表单参数
      form: {
        opinion: ""
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询申请表列表 */
    getList() {
      this.loading = true;
      // 复制一份参数，去掉 applicationType
      const { applicationType, ...params } = this.queryParams;

      if (this.queryParams.applicationType === "1") {
        // 只查大学生科技园退租
        getexitApplicationForm(params).then(response => {
          this.exitApplicationList = response.data.records.map(item => ({ ...item, applicationType: "1" }));
          this.total = response.data.total;
          this.loading = false;
        });
      } else if (this.queryParams.applicationType === "2") {
        // 只查众创空间退出
        // enterpriseName 需要转成 enterpriseTeam
        const params2 = { ...params, enterpriseTeam: params.enterpriseName };
        delete params2.enterpriseName;
        getexitCreateSpaceForm(params2).then(response => {
          this.exitApplicationList = response.data.records.map(item => ({ ...item, applicationType: "2" }));
          this.total = response.data.total;
          this.loading = false;
        });
      } else {
        // 不选类型时，查全部
        const params1 = { ...params };
        const params2 = { ...params, enterpriseTeam: params.enterpriseName };
        delete params2.enterpriseName;
        Promise.all([
          getexitApplicationForm(params1),
          getexitCreateSpaceForm(params2)
        ]).then(([scienceParkRes, innovationSpaceRes]) => {
          this.exitApplicationList = [
            ...scienceParkRes.data.records.map(item => ({ ...item, applicationType: "1" })),
            ...innovationSpaceRes.data.records.map(item => ({ ...item, applicationType: "2" }))
          ];
          this.total = this.exitApplicationList.length;
          this.loading = false;
        });
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        opinion: ""
      };
    },
    /** 搜索框操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 查看按钮操作 */
    handleView(row) {
      this.reset();
      const id = row.id;
      if (row.applicationType === "1") {
        getexitApplicationFormDetail(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看大学生科技园退租申请";
        });
      } else {
        getexitCreateSpaceFormDetail(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看众创空间退出申请";
        });
      }
    },
    /** 审核按钮操作 */
    handleApprove(row) {
      this.handleView(row);
    },
    /** 提交审核 */
    handleApproveSubmit(status) {
      const data = {
        ...this.form,
        status: status,
        opinion: this.form.opinion
      };
      if (this.queryParams.applicationType === "1") {
        editexitApplicationForm(data).then(response => {
          this.$message.success("审核成功");
          this.open = false;
          this.queryParams.applicationType = null;
          this.getList();
        });
      } else {
        editexitCreateSpaceForm(data).then(response => {
          this.$message.success("审核成功");
          this.open = false;
          this.queryParams.applicationType = null;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$confirm('是否确认删除该申请？').then(() => {
        if (row.applicationType === "1") {
          return delexitApplicationForm(id);
        } else {
          return delexitCreateSpaceForm(id);
        }
      }).then(() => {
        this.getList();
        this.$message.success("删除成功");
      }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.header {
  font-size: 16px;
  font-weight: bold;
  margin-bottom: 20px;
}
.el-card {
  margin-bottom: 20px;
}
</style>
